<?php
// 仅当会话尚未启动时调用 session_start()
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'reader') {
    header('Location: http://localhost/Library_management_system/index.php');
    exit();
}

// 数据库连接
try {
    $pdo = new PDO('mysql:host=localhost;dbname=library', 'root', '123456');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("数据库连接失败: " . $e->getMessage());
}

// 获取当前用户的 u_No
$u_No = $_SESSION['u_No'];

// 查询用户的违规记录及其类型
$sql = "
    SELECT vr.violation_time, vrt.ViolationRecord_type
    FROM ViolationRecord AS vr
    JOIN ViolationRecord_type AS vrt ON vr.violation_ID = vrt.ViolationRecord_ID
    WHERE vr.u_No = :u_No
";

$stmt = $pdo->prepare($sql);
$stmt->bindParam(':u_No', $u_No, PDO::PARAM_INT);
$stmt->execute();
$violations = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>查看违规记录</title>
</head>
<body>
    <h2>查看违规记录</h2>
    <ul>
        <?php if (count($violations) > 0): ?>
            <?php foreach ($violations as $violation): ?>
                <li>违规类型: <?php echo htmlspecialchars($violation['ViolationRecord_type']); ?>, 违规日期: <?php echo htmlspecialchars($violation['violation_time']); ?></li>
            <?php endforeach; ?>
        <?php else: ?>
            <li>没有违规记录。</li>
        <?php endif; ?>
    </ul>
</body>
</html>
